package com.rongshu.sendmessage.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.sendmessage.R;

public class StretchyTextView extends LinearLayout implements View.OnClickListener {
    /**
     * 默认的最大行数
     */
    private static final int MAX_LINES = 3;
    //无状态
    private static final int SPREADTEXT_STATE_NONE = 0;
    //缩回状态
    private static final int SPREADTEXT_STATE_RETRACT = 1;
    //展开状态
    private static final int SPREADTEXT_STATE_SPREAD = 2;

    private int maxLineCount = MAX_LINES;

    private int mState;
    //实际的行数
    private int actually_counts;
    /**
     * 显示内容的文本
     */
    private TextView tv_stretchy_content;
    /**
     * 显示操作的文本
     */
    private TextView tv_stretchy_bottom;

    private LinearLayout ll_stretchy_content;
    private InnerRunnable runable;
    private boolean flag = false;

    public StretchyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //填充布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_tv_stretchy, this);
        view.setPadding(0, 0, 0, 0);
        tv_stretchy_content = (TextView) view.findViewById(R.id.tv_stretchy_content);
        tv_stretchy_bottom = (TextView) view.findViewById(R.id.tv_stretchy_bottom);
        ll_stretchy_content = (LinearLayout) view.findViewById(R.id.ll_stretchy_content);
        ll_stretchy_content.setOnClickListener(this);
        ll_stretchy_content.setVisibility(GONE);
        setBottomTextGravity(Gravity.CENTER_HORIZONTAL);
        runable = new InnerRunnable();
    }

    /**
     * 设置展开标识的显示位置
     *
     * @param gravity
     */
    public void setBottomTextGravity(int gravity) {
        ll_stretchy_content.setGravity(gravity);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            Log.i("test","当前lines:"+tv_stretchy_content.getLineCount());
            Log.i("test","实际行数:"+actually_counts);
            if (actually_counts<=MAX_LINES && tv_stretchy_content.getLineCount() <=MAX_LINES) {
                mState = SPREADTEXT_STATE_NONE;
                ll_stretchy_content.setVisibility(View.GONE);
                tv_stretchy_content.setMaxLines(MAX_LINES + 1);
            } else {
                post(runable);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();
    }

    /**
     * 设置最大行数
     *
     * @param maxLineCount
     */
    public void setMaxLineCount(int maxLineCount) {
        this.maxLineCount = maxLineCount;
    }

    /**
     * 设置文本内容颜色
     *
     * @param color
     */
    public void setContentTextColor(int color) {
        this.tv_stretchy_content.setTextColor(color);
    }

    /**
     * 设置文本内容字体大小
     *
     * @param size
     */
    public void setContentTextSize(float size) {
        this.tv_stretchy_content.setTextSize(size);
    }

    /**
     * 设置文本内容
     *
     * @param charSequence
     */
    public final void setContent(CharSequence charSequence) {
        tv_stretchy_content.setText(charSequence, TextView.BufferType.NORMAL);
        post(new Runnable() {
            @Override
            public void run() {
                actually_counts = tv_stretchy_content.getLineCount();
                Log.i("test", "获取实际行数:" + actually_counts);
            }
        });
        mState = SPREADTEXT_STATE_SPREAD;
        flag = false;
        requestLayout();
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            if (mState == SPREADTEXT_STATE_SPREAD) {
                tv_stretchy_content.setMaxLines(MAX_LINES);
                ll_stretchy_content.setVisibility(View.VISIBLE);
                tv_stretchy_bottom.setText("展开");
                mState = SPREADTEXT_STATE_RETRACT;
            } else if (mState == SPREADTEXT_STATE_RETRACT) {
                tv_stretchy_content.setMaxLines(Integer.MAX_VALUE);
                ll_stretchy_content.setVisibility(View.VISIBLE);
                tv_stretchy_bottom.setText("收起");
                mState = SPREADTEXT_STATE_SPREAD;
            }
        }
    }
}
